package com.mpb.prj.practice;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.regex.Pattern;

import org.expasy.jpl.utils.file.JPLFileFinder;

public class JPLRunXTandemBatchMain {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		JPLRunXTandemBatchMain obj = new JPLRunXTandemBatchMain ();
		obj.runXTandem(args[0],args[1],args[2],args[3],args[4]);
	}
	
	/*
	 *  C:/DATA/UDAY/SWETHA/project/XtandemParams/XTandem
	 * C:/DATA/UDAY/SWETHA/project/XtandemParams/Data
	 * C:/DATA/UDAY/SWETHA/project/XtandemParams/IN
	 * 2008_12_11_ALS_09_LY01_J02.mgf
	 * C:/DATA/UDAY/SWETHA/project/XtandemParams/Data/OUT
	 * 
	 */
	
	public void runXTandem(String xTandemFolder, String xTandemInputXML, String inputDir, String regex, String outputDir ){
	
		if (!outputDir.endsWith("\\")) outputDir = outputDir+"\\";
		if (File.separatorChar=='\\') {
			// RegExp were not developed on windows, see below 
			outputDir = outputDir.replaceAll("\\\\", "\\\\\\\\");
		}
		/** List of all spectrum files */
		ArrayList<File> files = new ArrayList<File>();
		
		try {
			files = JPLFileFinder.getFiles(inputDir, regex);
		}
		catch(FileNotFoundException e) {
			System.out.println(e.toString());
		}
		
		ArrayList<String> inputLines = JPLRunXTandemBatchMain.readInputXML(xTandemInputXML);	
		int inputFileLineIdx = 0;
		int outputFileLineIdx = 0;
		int cnt = 0;
		for (String line : inputLines) {
			if (Pattern.matches(".*label=\"spectrum, path\".*", line)) {
				inputFileLineIdx = cnt;
			}
			if (Pattern.matches(".*label=\"output, path\".*", line)) {
				outputFileLineIdx = cnt;
			}
			cnt++;
		}
		
		for (File file : files) {
			String filePath = file.getAbsolutePath();
			if (File.separatorChar=='\\') {
				// RegExp were not developed on windows, see below 
				filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
			}
			
			String inputLine = inputLines.get(inputFileLineIdx);
			inputLine = inputLine.replaceAll(">[^<]+<", ">"+filePath+"<");
			
			
			filePath = outputDir+file.getName();
			filePath = filePath.replaceAll("\\.[A_Za-z]+$", "_XTandem.xml");
			String outputLine = inputLines.get(outputFileLineIdx);
			outputLine = outputLine.replaceAll(">[^<]+<", ">"+filePath+"<");
			
			FileWriter tmpInputFile = null;
			try {
				tmpInputFile = new FileWriter(outputDir+"JPLTempInputXML.xml",false);
				cnt = 0;
				for (String line : inputLines) {
					if (cnt==inputFileLineIdx) {
						tmpInputFile.write(inputLine+"\n");
					} else if (cnt==outputFileLineIdx) {
						tmpInputFile.write(outputLine+"\n");
					} else {
						tmpInputFile.write(line+"\n");
					}
					cnt++;
				}
				tmpInputFile.close();
				
				Runtime rt=Runtime.getRuntime(); 
				String cmd = "cmd /c \"" + xTandemFolder + "\\tandem.exe "+outputDir+"JPLTempInputXML.xml\" > tandem_process.txt";
				System.out.println("Searching : "+ file.getAbsolutePath()+" :");
				System.out.println(cmd);
				Process p = rt.exec(cmd);
				p.waitFor();
			}
			catch(Exception e) {
				System.out.println("Cannot write file : JPLTempInputXML.xml");				
			}
		}
	}
	
	private static ArrayList<String> readInputXML(String inputXML)
	{
		ArrayList<String> lines = new ArrayList<String>();

		File file = new File(inputXML);
		BufferedReader bufRdr  = null;
		String line = "";
		try {
			bufRdr  = new BufferedReader(new FileReader(file));
			while((line = bufRdr.readLine()) != null) {
				lines.add(line);
			}
		}
		catch(Exception e) {
			System.out.println("Cannot read input file : "+inputXML);
		}

		return lines;
	}

}
